//
// Created by Felix on 24-4-23.
//
#include "IndexControllerTest.hpp"

#include "app/TestComponent.hpp"
#include "oatpp-test/web/ClientServerTestRunner.hpp"
#include "oatpp/web/client/HttpRequestExecutor.hpp"

#include "controller/IndexController.hpp"
#include "app/IndexApiTestClient.hpp"

void IndexControllerTest::onRun() {
    TestComponent component;
    oatpp::test::web::ClientServerTestRunner runner;
    runner.addController(std::make_shared<IndexController>());
    runner.run([this, &runner] {
        OATPP_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider);
        OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper);

        auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(clientConnectionProvider);
        auto client = IndexApiTestClient::createShared(requestExecutor, objectMapper);
        auto response = client->getHello();

        OATPP_ASSERT(response->getStatusCode() == 200);

        auto message = response->readBodyToDto<oatpp::Object<MessageDto<> > >(objectMapper.get());

        /* Assert that received message is as expected */
        OATPP_ASSERT(message);
        OATPP_ASSERT(message->statusCode == 200);
        OATPP_ASSERT(message->message == "Hello World!");

    }, std::chrono::minutes(10) /* test timeout */);

    std::this_thread::sleep_for(std::chrono::seconds(1));
}
